package com.song.promoter.tianya;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class Tianya {
	Log log = LogFactory.getLog(Tianya.class);
	private HttpClient httpClient = new DefaultHttpClient(new PoolingClientConnectionManager());
	String loginUrl = "http://passport.tianya.cn/login";
	
	private boolean login(String username,String password) throws ClientProtocolException, IOException, ScriptException{
		HttpPost postMethod = new HttpPost(loginUrl);
		List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 
		pairs.add(new BasicNameValuePair("Submit", "登录"));
		pairs.add(new BasicNameValuePair("fowardURL", "http://www.tianya.cn"));
		pairs.add(new BasicNameValuePair("from", ""));
		pairs.add(new BasicNameValuePair("method", "name"));
		pairs.add(new BasicNameValuePair("returnURL", ""));
		pairs.add(new BasicNameValuePair("vpassword", password));
		pairs.add(new BasicNameValuePair("vwriter", username));
		postMethod.setEntity(new UrlEncodedFormEntity(pairs, Consts.UTF_8));
		//head
		postMethod.addHeader("Referer","http://passport.tianya.cn/login.jsp");
		postMethod.addHeader("Host","passport.tianya.cn");
		postMethod.addHeader("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28");
		postMethod.addHeader("Cookie","__ptime=" + System.currentTimeMillis());
		HttpResponse response = httpClient.execute(postMethod);
		String req1Str = EntityUtils.toString(response.getEntity());
		if(null != req1Str && req1Str.contains("登录中")){
			log.info("登录天涯第一步成功...");
		}else{
			log.error("登录天涯第一步失败...");
		}
		StringBuffer cookiesBuffer = new StringBuffer();
		ScriptEngineManager factory = new ScriptEngineManager();  
	    ScriptEngine engine = factory.getEngineByName("JavaScript");  
		String sb1 = StringUtils.substringBetween(req1Str.trim(), "<script>", "</script>");
		String[]strArr = sb1.split("document.cookie=");
		for (String str : strArr) {
			if(str != null && !"".equals(str.trim())){
				cookiesBuffer.append(engine.eval(str)).append(";");
			}
		}
		log.info("cookies:" + cookiesBuffer);
		String[] strArry = StringUtils.substringsBetween(req1Str, "src=\"", "\">");
		if(strArry == null || strArry.length < 2){
			log.error("天涯登录第二步出错.");
		}
		HttpGet getMethod1 = new HttpGet(strArry[0]);
		getMethod1.addHeader("Host","passport.tianyaclub.com");
		getMethod1.addHeader("Referer","http://passport.tianya.cn/login");
		getMethod1.addHeader("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28");
		httpClient.execute(getMethod1);
		log.info("访问:passport.tianyaclub.com成功");
		HttpGet getMethod2 = new HttpGet(strArry[2]);
		getMethod2.addHeader("Host","passport.hainan.net");
		getMethod2.addHeader("Referer","http://passport.tianya.cn/login");
		getMethod2.addHeader("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28");
		httpClient.execute(getMethod2);
		log.info("访问:passport.hainan.net成功");
		HttpGet getMethod3 = new HttpGet(strArry[2]);
		getMethod3.addHeader("Host","passport.hiholiday.com");
		getMethod3.addHeader("Referer","http://passport.tianya.cn/login");
		getMethod3.addHeader("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28");
		httpClient.execute(getMethod3);
		log.info("访问:passport.hiholiday.com成功");
		
		HttpGet getMethod = new HttpGet("http://www.tianya.cn/46344313");
		getMethod.addHeader("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28");
		getMethod.addHeader("Cookie",cookiesBuffer.toString());
		HttpResponse getResponse = httpClient.execute(getMethod);
		String req2Str = EntityUtils.toString(getResponse.getEntity());
		if(req2Str != null && req2Str.contains("")){
			log.info("天涯登录成功...");
			return true;
		}else{
			log.info("天涯登录失败..................");
			return false;
		}
	}

	public static void main(String[] args) throws Exception {
		Tianya tianya = new Tianya();
		tianya.login("506610", "3151987");
	}
}
